/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.alvin.api.ui;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.alvin.api.service.IPlatFormApiService;
import org.alvin.api.ui.renderer.PlatTypeItem;
import org.alvin.api.utils.ClusterService;
import org.alvin.api.utils.ServiceObjectFactory;

/**
 *
 * @author tangzhichao
 */
public class PlatPanel extends javax.swing.JPanel {

    private PlatTypeItem[] items = new PlatTypeItem[]{
        new PlatTypeItem("阿里云--ECS 云服务器", ClusterService.TYPE_ALIYUN, "https://help.aliyun.com/product/8314827_25365.html"),
        new PlatTypeItem("腾讯云--云服务器", ClusterService.TYPE_QCLOUD, "https://www.qcloud.com/doc/api/229"),
        new PlatTypeItem("UCloud--云主机云物理机", ClusterService.TYPE_UCLOUD, "https://docs.ucloud.cn/api-docs/index.html"),};

    private IPlatFormApiService getApiServiceByPlatForm(String platform) throws Exception {
        return ServiceObjectFactory.getInstance(platform, IPlatFormApiService.class);
    }

    /**
     * Creates new form PlatPanel
     */
    public PlatPanel() {
        initComponents();
        jComboBox1.setModel(new DefaultComboBoxModel(items));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        commentBox = new javax.swing.JCheckBox();
        nullBox = new javax.swing.JCheckBox();
        filedBox = new javax.swing.JCheckBox();
        onlyNameBox = new javax.swing.JCheckBox();
        strVarBox = new javax.swing.JCheckBox();
        privateKeyWork = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel1 = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTabbedPane1.addTab("请求参数列表", jScrollPane1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jTabbedPane1.addTab("公共参数", jScrollPane2);

        jLabel2.setText("API名称(如 创建实例)：");

        jButton1.setText("获取");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        commentBox.setText("输出注释");

        nullBox.setText("输出是否必须");

        filedBox.setText("首字母小写");

        onlyNameBox.setText("只要参数名");

        strVarBox.setText("参数加双引号");

        privateKeyWork.setText("私有化关键字");

        jButton2.setText("浏览地址");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("平台：");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 701, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTextField1)
                            .addGap(18, 18, 18)
                            .addComponent(jButton1))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton2))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(commentBox)
                            .addGap(18, 18, 18)
                            .addComponent(nullBox)
                            .addGap(18, 18, 18)
                            .addComponent(filedBox)
                            .addGap(18, 18, 18)
                            .addComponent(onlyNameBox)
                            .addGap(18, 18, 18)
                            .addComponent(strVarBox)
                            .addGap(18, 18, 18)
                            .addComponent(privateKeyWork)
                            .addGap(21, 21, 21))))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(commentBox)
                    .addComponent(nullBox)
                    .addComponent(filedBox)
                    .addComponent(onlyNameBox)
                    .addComponent(strVarBox)
                    .addComponent(privateKeyWork))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(33, 33, 33)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        jTextArea1.setText("");
        jTextArea2.setText("");
        jTabbedPane1.setSelectedIndex(0);
        if (this.jTextField1.getText().trim().isEmpty()) {
            return;
        }

        PlatTypeItem item = (PlatTypeItem) jComboBox1.getSelectedItem();

        IPlatFormApiService platForm = null;
        try {
            platForm = getApiServiceByPlatForm(item.getValue());
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "该平台功能尚未实现");
            return;
        }
        item.setComment(commentBox.isSelected());
        item.setHasNull(nullBox.isSelected());
        item.setJavaFiled(filedBox.isSelected());
        item.setOnlyName(onlyNameBox.isSelected());
        item.setToStringVar(strVarBox.isSelected());
        item.setSearchText(this.jTextField1.getText().trim());
        item.setPrivateKeyWork(privateKeyWork.isSelected());
        try {
            Map<String, String> content = platForm.getRequestParams(item);
            jTextArea1.setText(content.get("request"));
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "你搜索的关键字不存在");
        }
        jTextArea2.setText(platForm.getPublicParmas());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        PlatTypeItem item = (PlatTypeItem) jComboBox1.getSelectedItem();
        try {
            Desktop.getDesktop().browse(new URI(item.getUrl()));
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox commentBox;
    private javax.swing.JCheckBox filedBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JCheckBox nullBox;
    private javax.swing.JCheckBox onlyNameBox;
    private javax.swing.JCheckBox privateKeyWork;
    private javax.swing.JCheckBox strVarBox;
    // End of variables declaration//GEN-END:variables
}
